package com.cskaoyan.controller;

import com.cskaoyan.annotation.AdminLog;
import com.cskaoyan.bean.Admin;
import com.cskaoyan.bean.basebean.BaseParam;
import com.cskaoyan.bean.basebean.BaseRespVo;
import com.cskaoyan.bean.sysmanage.*;
import com.cskaoyan.exception.MarketException;
import com.cskaoyan.service.AdminService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("admin")
public class AdminController {
    @Autowired
    AdminService adminService;

    //查询管理员列表
    @RequiresPermissions(value = "admin:admin:list")
    @RequestMapping("admin/list")
    public BaseRespVo getAdminList(String username, BaseParam baseParam) {
        AdminPageData adminPageData = adminService.query(username, baseParam);
        return BaseRespVo.ok(adminPageData);
    }

    @RequestMapping("profile.nnotice")
    public BaseRespVo getNnotice(){
        return BaseRespVo.ok();
    }

    //获取notice
    @RequestMapping("profile/lsnotice")
    public BaseRespVo getNotice(){
        return BaseRespVo.ok();
    }

    //获取管理员roleId信息
    @RequestMapping("role/options")
    public BaseRespVo getOptions() {
        OptionsVo optionsVo = adminService.queryAllRole();
        return BaseRespVo.ok(optionsVo);
    }

    //管理员头像上传
    @RequiresPermissions(value = "admin:storage:create")
    @PostMapping("storage/create")
    public BaseRespVo create(MultipartFile file) throws MarketException {
        StorageVo storageVo = adminService.addStorage(file);
        return BaseRespVo.ok(storageVo);
    }

    //新建管理员用户
    @AdminLog(action = "添加管理员")
    @RequiresPermissions(value = "admin:admin:create")
    @PostMapping("admin/create")
    public BaseRespVo addAdmin(@RequestBody AddAdminBo addAdminBo) {
        //判断用户名是否存在
        boolean isExist = adminService.queryAdminByUserName(addAdminBo.getUsername());
        if (isExist) {
            return BaseRespVo.fail("用户名已存在，请更换用户名");
        }
        boolean result = adminService.addAdmin(addAdminBo);
        if (result) {
            return BaseRespVo.ok();
        }

        return BaseRespVo.fail("新建管理员用户失败");
    }

    //编辑管理员用户
    @AdminLog(action = "编辑管理员")
    @RequiresPermissions(value = "admin:admin:update")
    @PostMapping("admin/update")
    public BaseRespVo updateAdmin(@RequestBody UpdateAdminBo updateAdminBo) {


        //判断用户名是否存在
        boolean isExist = adminService.queryAdminByUserName(updateAdminBo.getUsername());
        if (isExist) {
            //判断是否为原用户名
            Admin admin = adminService.queryAdminById(updateAdminBo.getId());
            if (!updateAdminBo.getUsername().equals(admin.getUsername())) {
                return BaseRespVo.fail("用户名已存在，请更换用户名");
            }
        }
        boolean result = adminService.updateAdmin(updateAdminBo);
        if (result) {
            return BaseRespVo.ok();
        }

        return BaseRespVo.fail("新建管理员用户失败");
    }

    //删除管理员账号
    @AdminLog(action = "删除管理员")
    @RequiresPermissions(value = "admin:admin:delete")
    @PostMapping("admin/delete")
    public BaseRespVo deleteAdmin(@RequestBody UpdateAdminBo updateAdminBo) {
        boolean result = adminService.deleteAdmin(updateAdminBo.getId());
        if (result) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail("删除失败");
    }
}
